<?php
$this->breadcrumbs=array(
    'Lookups'=>array('index'),
);

if ($this->get('Lookup[type]'))
    $this->breadcrumbs[ucfirst($this->get('Lookup[type]'))] = array('index', 'Lookup[type]' => $this->get('Lookup[type]')); 
$this->breadcrumbs[] = 'Manage';
if(Lookup::firstType()!=null)
    $this->menu=array(
        array('label'=>'Create', 
            'url'=>array('create', 'Lookup[type]' =>Lookup::firstType()),
            'linkOptions' => array('class' => 'fancy-iframe')),
    );

Yii::app()->clientScript->registerScript('search', "
$('.create-type-button').click(function(){
    $('.create-type-form').toggle();
    return false;
});
");
?>

<h1>
<?php 
/*if ($this->get('Lookup[type]',''))
    echo 'Manage '.ucfirst($model->type);
else*/
    echo 'Manage Lookups';
?>
</h1>

    <div style="margin-left: 300px">
    <label>Select type </label>
    <?php echo (Lookup::firstType()!=null)? CHtml::dropDownList('Lookup[type]',Lookup::firstType(),Lookup::types(),array('class'=>'types')):'';?>
    <?php echo CHtml::link('Add new type','#',array('class'=>'create-type-button'));?>
    </div>
    <script type="text/javascript">
    $(document).ready(function(){
        $('div select#Lookup_type.types').bind('change',function(){
            var type=$('div select#Lookup_type.types').val();
            $.fn.yiiGridView.update('lookup-grid', { data : { 'Lookup[type]' : type } });
        });
        
    });
    </script>
<div class="create-type-form" style="display:none;margin-left: 157px;">
<?php $this->renderPartial('create_type',array('model'=>new Lookup(),)); ?>
</div>

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'lookup-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'selectableRows'=>2,
    'selectionChanged'=>"updateSelectors",
    'afterAjaxUpdate'=>"js:function(){
            var type=$('div select#Lookup_type.types').val();
            var url='".baseUrl()."?r=Admin/lookup/create&Lookup[type]='+type;
            $('div#sidebar.crud-menu ul#yw1 li a').first().attr('href',url);
            enableSort();
            $('#lookup-grid td.button-column a.update').attr('class','fancy-iframe');
            //initFancybox();
        }",
    'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'3%'),
        ),
        //'id',
        'name',
        'code',
        //'type',
        //'position',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array('label' => 'Delete selected items', 'url'=>$this->createUrl('delete'), 'linkOptions' => array('onclick'=>'return multipleDelete("lookup-grid",this.href)'));
}
Yii::app()->clientScript->registerScriptFile(Yii::app()->core->AssetUrl.'/scripts/gridview.js', CClientScript::POS_BEGIN);

$cs=Yii::app()->getClientScript();
$cs->registerCssFile($cs->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
$cs->registerCoreScript('jquery.ui');

$script = "
function initSortable() {
    //append id to tr tag
    $.each(\$( 'table.items td.sortable input'), function(){
        $(this).parents('tr').attr('id', 'ids_'+$(this).val());
    });
    
    //init sortable
    $( 'table.items' ).sortable({
        handle : 'td.sortable', 
        items : 'tr:gt(1)',
        update: function(event, ui){
            var data = $('table.items').sortable('serialize');
            $.get('".$this->createUrl('order')."', data, function(res){
            });
        }
    });
    $( 'table.items' ).disableSelection();
}
function enableSort() {
    
        $.each(\$('.items tr:gt(1)'), function(){
            if (\$(this).find('td').length > 1)
                $(this).find('td:first').addClass('sortable');
        });
        initSortable();
}
enableSort();
$('#lookup-grid td.button-column a.update').attr('class','fancy-iframe');
//initFancybox();
";
Yii::app()->clientScript->registerScript(__CLASS__.'#InitSortable', $script, CClientScript::POS_READY);
?>